package com.laint.hear.ui.activity;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.Menu;

import com.laint.hear.R;
import com.laint.hear.ui.base.BaseActivity;
import com.laint.hear.ui.fragment.BeautyFragment;
import com.laint.hear.ui.fragment.JokesFragment;
import com.laint.hear.ui.fragment.MoreFragment;
import com.laint.hear.ui.fragment.NewsFragment;
import com.laint.hear.utils.ConstantsValue;
import com.laint.hear.utils.ToastUtils;
import com.laint.hear.view.RVPIndicator;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;

public class MainActivity extends BaseActivity {

    @BindView(R.id.main_viewpager)
    ViewPager mViewPager;

    @BindView(R.id.main_indicator)
    RVPIndicator mIndicator;
    private ArrayList<Fragment> mFlists = new ArrayList<>();
    private List<String> mDatas;

    private long currentBackPressedTime = 0; // 退出时间
    private static final int BACK_PRESSED_INTERVAL = 2000; // 退出间隔
    @Override
    protected void setUpContentView() {
        setContentView(R.layout.activity_main,R.string.empty,MODE_HOME);
        toolbar.setLogo(R.mipmap.logo);
    }
    @Override
    protected void setUpData() {
        mFlists.add(new NewsFragment());
        mFlists.add(new JokesFragment());
        mFlists.add(new BeautyFragment());
        mFlists.add(new MoreFragment());
        FragmentPagerAdapter adapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public int getCount() {
                return mFlists.size();
            }

            @Override
            public Fragment getItem(int position) {
                return mFlists.get(position);
            }
        };

        mDatas = Arrays.asList(getResources().getStringArray(R.array.home_tabs));

        mIndicator.setTabItemTitles(mDatas);
        mViewPager.setAdapter(adapter);
        mViewPager.setOffscreenPageLimit(3);
        mIndicator.setViewPager(mViewPager, 0);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        switch (intent.getIntExtra(ConstantsValue.APP_STATUS,100)){
            case ConstantsValue.KILLED:
                Intent intent1 = new Intent(this, WelcomActivity.class);
                startActivity(intent1);
                finish();
                break;
        }
    }
    @Override
    protected void protectApp() {
        Intent intent = new Intent(this, WelcomActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_menu, menu);
        return true;
    }


    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN
                && event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            if (System.currentTimeMillis() - currentBackPressedTime > BACK_PRESSED_INTERVAL) {
                currentBackPressedTime = System.currentTimeMillis();
                ToastUtils.showShort(this,getString(R.string.tips_exit));
                return true;
            } else {
                finish(); // 退出
            }
        }else if(event.getKeyCode() == KeyEvent.KEYCODE_MENU){
            return true;
        }
        return super.dispatchKeyEvent(event);
    }
}
